package edu.zhang.front.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.cj.Session;

import edu.zhang.dao.MenuDAO;
import edu.zhang.front.po.ShoppingCar;
import edu.zhang.po.Menu;

@WebServlet("/customer/ShoppingCarInsert")
public class ShoppingCarInsert extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private MenuDAO menuDAO = new MenuDAO();
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String opt = request.getParameter("opt");
		if (opt.equals("create")) {
			create(request, response);
		}else if (opt.equals("add")) {
			add(request, response);
		}else if (opt.equals("reduce")) {
			reduce(request, response);
		}else if (opt.equals("remove")) {
			remove(request, response);
		}
	
	}

	private void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String menuId = request.getParameter("menuid");
		HttpSession httpSession = request.getSession();
		List<ShoppingCar> list = (List<ShoppingCar>) httpSession.getAttribute("list");
		int index = -1;
		for (int i = 0;i < list.size(); i++) {
			if (list.get(i).getMenu().getId() == Integer.parseInt(menuId)) {
				index = i;
				break;
			}
		} 
		list.remove(index);
		request.getRequestDispatcher("HomepageMenu").forward(request, response);
	}

	private void reduce(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String menuId = request.getParameter("menuid");
		HttpSession httpSession = request.getSession();
		List<ShoppingCar> list = (List<ShoppingCar>) httpSession.getAttribute("list");
		int index = -1;
		for (int i = 0;i < list.size();i++) {
			if (list.get(i).getMenu().getId()==Integer.parseInt(menuId)) {
				index = i;
				break;
			}
		}
		if (index>=0) {
			ShoppingCar shoppingCar =  list.get(index);
			if ((shoppingCar.getSum()-1)<=0) {
				list.remove(index);
			}else {
				shoppingCar.setSum(shoppingCar.getSum()-1);
				list.set(index, shoppingCar);
			}
			
		}
		httpSession.setAttribute("list", list);
		
		request.getRequestDispatcher("HomepageMenu").forward(request, response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String menuId = request.getParameter("menuid");
		System.out.println(menuId);
		HttpSession httpSession = request.getSession();
		List<ShoppingCar> list = (List<ShoppingCar>) httpSession.getAttribute("list");
		int index = -1;
		for (int i = 0;i < list.size();i++) {
			if (list.get(i).getMenu().getId()==Integer.parseInt(menuId)){
				index = i;
				break;
			}
		}if (index>=0) {
			ShoppingCar shoppingCar =  list.get(index);
			shoppingCar.setSum(shoppingCar.getSum()+1);
			list.set(index, shoppingCar);
		}
		
		httpSession.setAttribute("list", list);
		
		request.getRequestDispatcher("HomepageMenu").forward(request, response);
	}

	private void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession httpSession = request.getSession();
		
		String menuId = request.getParameter("menuid");
		Menu menu = menuDAO.getMenuById(Integer.parseInt(menuId));
		
		Object sessionList = httpSession.getAttribute("list");
		List<ShoppingCar> list = null;
		if (sessionList==null) {
			list = new ArrayList<ShoppingCar>();
			ShoppingCar shoppingCar = new ShoppingCar();
			shoppingCar.setMenu(menu);
			shoppingCar.setCustomerId(1);
			shoppingCar.setSum(1);
			list.add(shoppingCar);
		}else {
			list = (List<ShoppingCar>) sessionList;
			int index = -1;
			for (int i = 0;i < list.size();i++) {
				if (list.get(i).getMenu().getId()==Integer.parseInt(menuId)) {
					index = i;
					break;
				}
			}if (index>=0) {
				ShoppingCar shoppingCar =  list.get(index);
				shoppingCar.setSum(shoppingCar.getSum()+1);
				list.set(index, shoppingCar);
			}else {
				ShoppingCar shoppingCar = new ShoppingCar();
				shoppingCar.setMenu(menu);
				shoppingCar.setCustomerId(1);
				shoppingCar.setSum(1);
				list.add(shoppingCar);
			}
		}
		
		
		httpSession.setAttribute("list", list);
		
		request.getRequestDispatcher("HomepageMenu").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		this.doGet(request, response);
	}

}
